#include "swagme.h"
#include "Character.h"
#include "Sprite.h"

const int Character::FrameCount = 3;

Character::Character(Sprite* _ps, int _x, int _y, double _unitsPerMs)
  : TouchableObject(_x, _y, 12, 12),
  Facing(sm_dir_right), prevFacing(sm_dir_right), FrameCounter(0) {
  Init(_ps,_x,_y,_unitsPerMs);
}

Character::Character(Sprite* ps)
  : TouchableObject(0, 0, 12, 12), Facing(sm_dir_right), prevFacing(sm_dir_right), FrameCounter(0) {
  Init(ps,0,0,0.0);
}

Character::Character()
  : TouchableObject(0, 0, 12, 12), Facing(sm_dir_right), prevFacing(sm_dir_right), FrameCounter(0) {
  Init(NULL,0,0,0.0);
}

void Character::Init(Sprite* ps, int _x, int _y, double _unitsPerMs)  {
  mpSprite = ps;
  box.x = _x;
  box.y = _y;
  prevX = _x;
  prevY = _y;
  mUnitsPerMs = _unitsPerMs;
}

void Character::updatePrevPos() {
  prevX = getLeft();
  prevY = getTop();
}

int Character::Draw(SDL_Surface *dst, bool bShadow) {
  int frameNum ;

  if ( (getLeft() != prevX) || (getTop() != prevY)) {
    if (++FrameCounter > FrameCount * 4) FrameCounter = 0;
    frameNum = (FrameCounter /4) % FRAMES_PER_ANIM;
  } else {
    FrameCounter = 1;
    frameNum = 1;
  }
  return mpSprite->Draw(Facing -1, frameNum,dst, (int)(getLeft()+MAP_POS_X), (int)(getTop()+MAP_POS_Y), bShadow);
}

// Facing has a setter and getter method so facing changes can be animated if need be
void Character::SetFacing(sm_direction f) {
  prevFacing = Facing;
  Facing = f;
}
sm_direction Character::GetFacing() {
  return Facing;
}

Character::~Character() {
}

void Character::move(double newLeft, double newTop) {
  box.x = newLeft;
  box.y = newTop;
}
